import {unioformDateTransform} from '../../utils/index.js'
import {handleSql} from '../../database/mysql/index.js'


class appVersionModel {
    constructor() {
        this.tableName = 'app_version';
    }


    // 获取app安装包以及版本信息_查询多条历史记录
    async getHistoryAppPackageInfoList(searchInfo){
        const {fileOwnership} = searchInfo
        let tmpfileOwnership = fileOwnership || '%'
        const sql = `SELECT * FROM ${this.tableName} WHERE  fileOwnership LIKE '${tmpfileOwnership}' `
        let result = await handleSql(sql)
        return result
    }



    // 获取app安装包以及版本信息,模糊查询，只查询一条
    async getAppPackageInfo(searchInfo){
        const {id,isAbandon,fileOwnership} = searchInfo
        let tmpId = id || '%'

        let tmpfileOwnership = fileOwnership || '%'

        let tmpIsAbandon = isAbandon
        if(isAbandon !== 0 && isAbandon !== 1){
            tmpIsAbandon =  '%'
        }
        const sql = `SELECT * FROM ${this.tableName} WHERE id LIKE '${tmpId}' AND isAbandon LIKE '${tmpIsAbandon}' AND fileOwnership LIKE '${tmpfileOwnership}' `
        let result = await handleSql(sql)
        return result[0]
    }


    // 更新app安装包的版本信息
    async updateAppPackageInfo(searchKey,infoData){
        const {version,filePath} = infoData
        const {id,isAbandon,fileOwnership} = searchKey
        
        let tmpId = id || '%'

        let tmpIsAbandon = isAbandon
        if(isAbandon !== 0 && isAbandon !== 1){
            tmpIsAbandon =  '%'
        }

        const sql = `UPDATE ${this.tableName} SET ? WHERE id LIKE '${tmpId}' AND isAbandon LIKE '${tmpIsAbandon}' AND fileOwnership LIKE '${fileOwnership}' `
        await handleSql(sql,[infoData])
        return true
    }

    // 新增app安装包版本信息
    async addAppPackageInfo(infoData){
        const {version,filePath,fileName,fileOwnership} = infoData
        const sql = `INSERT INTO ${this.tableName}(version,filePath,fileName,isAbandon,createDate,fileOwnership) 
                     VALUES (?,?,?,?,?,?)`
        let result = await handleSql(sql,[version,filePath,fileName,false,unioformDateTransform(new Date()),fileOwnership])

        if(result.affectedRows == 1){
            return true
        }else{
            return false
        }
    }

    // 删除app信息
    async deleteAppPackageInfo(id){
        const sql = `DELETE FROM ${this.tableName} WHERE id = ?`
        let result =  await handleSql(sql,[id])
        if(result.affectedRows == 1){
            return true
        }else{
            return false
        }
    }
}   

export default new appVersionModel();